<?php

final class ImportUtils {
    
    const SEP = 'xx00xx';

    public static function importSettledContract() {
        $id = Utils::getValPost('id');
        $insert_timestamp = Utils::getValPost('insert_timestamp');
        $modify_timestamp = Utils::getValPost('modify_timestamp');
        $bet_amount = Utils::getValPost('bet_amount');
        $bet_slip = Utils::getValPost('bet_slip');
        $current_goal = Utils::getValPost('current_goal');
        $dangerous = Utils::getValPost('dangerous');
        $result_amount = Utils::getValPost('result_amount');
        $running = Utils::getValPost('running');
        $settled_play_time = Utils::getValPost('settled_play_time');
        $settled_total_goals = Utils::getValPost('settled_total_goals');
        $under_goal = Utils::getValPost('under_goal');
        $won = Utils::getValPost('won');
        $won_amount = Utils::getValPost('won_amount');
        $contract_id = Utils::getValPost('contract_id');
        $current_time = Utils::getValPost('current_time');
        $away_name = Utils::getValPost('away_name');
        $home_name = Utils::getValPost('home_name');
        $rate = Utils::getValPost('rate');
        $leagueId = Utils::getValPost('league_id');
        $DAL = new DALSettledContract();
        $contract = new SettledContract($id, $insert_timestamp, $modify_timestamp, $bet_amount, $bet_slip, $current_goal, 
                                        $dangerous, $result_amount, $running, $settled_play_time, $settled_total_goals, 
                                        $under_goal, $won, $won_amount, $contract_id, $current_time, $away_name, $home_name, $rate, $leagueId);
        $obj = Utils::objectToArray($contract);
        echo $DAL->save($obj);
    }

    public static function updateUserBalance() {
        $id = Utils::getValPost('userId');
        $balance = Utils::getValPost('balance');
        $dalUserAccount = new DALUserAccount();
        return $dalUserAccount->updateBalance($id, $balance);
    }
    
    public static function updateCrawlData() {
        $data = Utils::getValPost('data');
        $dataObj = json_decode($data);
        $dalCountry = new DALCountry();
        return $dalCountry->parseAndPersistCountry($dataObj);
    }
    
    public static function updateLeagueStatus(){
        $countries = isset($_POST['countries']) ? $_POST['countries'] : array();
        $leagues = isset($_POST['leagues']) ? $_POST['leagues'] : array() ;
        $setting = isset($_POST['setting']) ? $_POST['setting'] : array();
        
        try {
            if(is_array($countries) && count($countries) > 0){
                $dalCountry = new DALCountry();
                $dalCountry->updateCountryConfig($countries);
            }
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }

        try {
            if(is_array($leagues) && count($leagues) > 0){
                    $DAlLeague = new DALLeague();
                    $DAlLeague->updateLeagueConfig($leagues);
            }
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
        
        try {
            $dalConfig = new DALConfig();
            $dalConfig->save($setting);
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
        return 1;
    }
    
    public static function updateStatus(){
        $id = Utils::getValPost('id');
        $status = Utils::getValPost('status');
        $dalUser = new DALUserAccount();
        return $dalUser->updateAppStatus($id, $status);
    }

    public static function validateImportKey() {
        $key = Utils::getValPost('validate');
        if ($key && $key = Config::IMPORT_KEY) {
            return true;
        } 
        return false;
    }

    public static function updateSetting(){
        $data = Utils::getValPost('setting');
        $dal = new DALConfig();
        return $dal->save($data);
    }
}
